﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LingDong.ConfigProvider;
using System.IO;

namespace LingDong.IndexMerger.Helper
{
    internal class SubIndexNameHelper
    {
        #region Document Index

        /// <summary>
        /// Get document index related files
        /// </summary>
        /// <param name="docIndex"></param>
        /// <param name="parsedDoc"></param>
        /// <param name="oriDoc"></param>
        /// <returns>max prefix index</returns>
        internal static int GetDocumentFileNameList(out List<string> docIndex, out List<string> parsedDoc, out List<string> oriDoc)
        {
            docIndex = new List<string>();
            parsedDoc = new List<string>();
            oriDoc = new List<string>();

            int prefixNumber = 0;
            while (IsAllDocumentIndexFileExists(prefixNumber))
            {
                docIndex.Add(GetDocumentIndexFileName(prefixNumber));
                parsedDoc.Add(GetParsedDocumentFileName(prefixNumber));
                oriDoc.Add(GetOriginalDocumentFileName(prefixNumber));
                prefixNumber++;
            }
            return prefixNumber;
        }

        /// <summary>
        /// Return true only if all document index related files exists
        /// </summary>
        /// <param name="curPrefix"></param>
        /// <returns></returns>
        private static bool IsAllDocumentIndexFileExists(int curPrefix)
        {
            string[] files = new string[] { 
                GetDocumentIndexFileName(curPrefix), 
                GetParsedDocumentFileName(curPrefix), 
                GetOriginalDocumentFileName(curPrefix) };
            foreach (string file in files)
            {
                if (!File.Exists(file))
                    return false;
            }
            return true;
        }

        #endregion
        

        #region Dictionary Index

        /// <summary>
        /// Get dictionary index related files
        /// </summary>
        /// <param name="dictionary"></param>
        /// <param name="invertedList"></param>
        /// <returns>max prefix index</returns>
        internal static int GetDictionaryFileNameList(out List<string> dictionary, out List<string> invertedList)
        {
            dictionary = new List<string>();
            invertedList = new List<string>();

            int prefixNumber = 0;
            while (IsAllDocumentIndexFileExists(prefixNumber))
            {
                dictionary.Add(GetDictionaryFileName(prefixNumber));
                invertedList.Add(GetInvertedListFileName(prefixNumber));
                prefixNumber++;
            }
            return prefixNumber;
        }

        /// <summary>
        /// Return true only if all dictionary index related files exists
        /// </summary>
        /// <param name="curPrefix"></param>
        /// <returns></returns>
        private static bool IsAllDictionaryIndexFileExists(int curPrefix)
        {
            string[] files = new string[] { 
                GetDictionaryFileName(curPrefix), 
                GetInvertedListFileName(curPrefix) };
            foreach (string file in files)
            {
                if (!File.Exists(file))
                    return false;
            }
            return true;
        }

        #endregion
        

        #region Private Methods

        private static string GetDictionaryFileName(int curPrefix)
        {
            return FormatSubIndexFileName(curPrefix, ConfigIndex.Default.DictionaryPostfix);
        }

        private static string GetInvertedListFileName(int curPrefix)
        {
            return FormatSubIndexFileName(curPrefix, ConfigIndex.Default.InvertedListPostfix);
        }

        private static string GetDocumentIndexFileName(int curPrefix)
        {
            return FormatSubIndexFileName(curPrefix, ConfigIndex.Default.DocumentIndexPostfix);
        }

        private static string GetParsedDocumentFileName(int curPrefix)
        {
            return FormatSubIndexFileName(curPrefix, ConfigIndex.Default.ParsedDocumentPostfix);
        }

        private static string GetOriginalDocumentFileName(int curPrefix)
        {
            return FormatSubIndexFileName(curPrefix, ConfigIndex.Default.OriginalDocumentPostfix);
        }

        private static string FormatSubIndexFileName(int curPrefix, string postfix)
        {
            return String.Format("{0}{1}{2}.{3}", 
                ConfigCommon.Default.LingDongDataRoot, ConfigIndex.Default.SubIndexDir, curPrefix, postfix);
        }

        #endregion
    }
}
